package com.yisu.expressway.onedollar.activity;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.widget.TitleView;
import com.yisu.expressway.utils.SpannableUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Kaven on 2016/12/30.
 */

public class PaySuccessActivity extends BasicActivity {
    private static final String EXTRA_KEY_ORDER_ID = "extra_key_order_id";
    private static final String EXTRA_KEY_GOODS_NO = "extra_key_goods_no";
    private static final String EXTRA_KEY_GOODS_NAME = "extra_key_goods_name";
    private static final String EXTRA_KEY_PERIODS_NUM = "extra_key_periods_num";
    private static final String EXTRA_KEY_JOIN_NUM = "extra_key_join_num";
    @Bind(R.id.title_bar)
    protected TitleView mTitleView;
    @Bind(R.id.tv_info)
    protected TextView mInfoTv;
	@Bind(R.id.tv_joinnum)
	protected TextView mJoinNumTv;
    private String mLuckyNums;

    private String mGoodsName;
    private long mOrderId;
    private long mPeriodsNum;
    private long mJoinNum;
	private long mGoodsNo;

	public static void launch(Context context, long orderId, String goodsName, long periodsNum,
                              long joinNum,long goodsNo) {
        Intent intent = new Intent(context, PaySuccessActivity.class);
        intent.putExtra(EXTRA_KEY_ORDER_ID, orderId);
        intent.putExtra(EXTRA_KEY_GOODS_NO, goodsNo);
        intent.putExtra(EXTRA_KEY_GOODS_NAME, goodsName);
        intent.putExtra(EXTRA_KEY_PERIODS_NUM, periodsNum);
        intent.putExtra(EXTRA_KEY_JOIN_NUM, joinNum);
        context.startActivity(intent);
    }

    @OnClick({ R.id.tv_order_join_again, R.id.tv_see_more, R.id.ll_order_info })
    public void onClick(View view) {
        switch (view.getId()) {
	        case R.id.tv_order_join_again:
		        jump2MainPage();
		        break;
	        case R.id.tv_see_more:
		        jump2RecordPage();
		        break;
	        case R.id.ll_order_info:
		        jump2OrderDetailPage();
		        break;
	        default:
		        break;
        }
    }

	private void jump2OrderDetailPage() {
		OrderDetailActivity.launch(this,mOrderId);
		finish();
	}

	private void jump2RecordPage() {
		startTargetActivity(OneDollarRecordActivity.class);
		finish();
	}

	private void jump2MainPage() {
		startTargetActivity(OneDollarMainActivity.class);
		finish();
	}

	@Override
	protected int getThemeId() {
		return R.style.OneDollarTheme;
	}

	@Override
    protected int getInflateLayoutId() {
        return R.layout.activity_pay_suc;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void releasePresenter() {

    }

    @Override
    protected void getIntentData(Intent intent) {
        mOrderId = intent.getLongExtra(EXTRA_KEY_ORDER_ID, -1);
        mGoodsName = intent.getStringExtra(EXTRA_KEY_GOODS_NAME);
        mPeriodsNum = intent.getLongExtra(EXTRA_KEY_PERIODS_NUM, -1);
        mJoinNum = intent.getLongExtra(EXTRA_KEY_JOIN_NUM, -1);
		mGoodsNo = intent.getLongExtra(EXTRA_KEY_GOODS_NO, -1);
    }

    @Override
    protected void getSavedBundleData(Bundle bundle) {

    }

	@Override
	protected void initViews() {
		String format = String.format(getString(R.string.one_dollar_info), mPeriodsNum,
			mGoodsName);
		mInfoTv.setText(format);
		format = String.format(getString(R.string.one_dollar_times), mJoinNum);
		mJoinNumTv.setText(format);
	}

    @Override
    protected void bindEvents() {
		mTitleView.setOnTitleBarClickEvent(new TitleView.OnTitleBarClickEvent() {
			@Override
			public void onLeftClick(View view) {
				jump2GoodsDetailPage();
			}

			@Override
			public void onTitleClick(View view) {

			}

			@Override
			public void onRightClick(View view) {

			}
		});
    }
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			jump2GoodsDetailPage();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	private void jump2GoodsDetailPage() {
		finishActivity(GoodsPurchaseActivity.class);
		GoodsPurchaseActivity.launch(this,mGoodsNo);
		finish();
	}

	@Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected void onLoginStateChange(boolean login) {

    }

}
